﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;
using DataLib.BLL;
using BarCode = DataLib.BLL.BarCode;
using InStorage = DataLib.BLL.InStorage;
using OutStorage = DataLib.Model.OutStorage;


namespace EWMSWeb.Production
{
    public partial class ProduceAudit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    User curuser = new User();
                    curuser = (User)Session["UserInfo"];
                    bool flag = true;
                    flag = SetDisplay();
                    if (flag == false)
                    {
                        DataLib.Common.MessageBox.ShowAndRedirect(Page, "非常URL，请从URL进入", "ProduceWaitInStorageList.aspx");
                    }

                }

            }

        }

        protected bool SetDisplay()
        {
            bool flag = true;
            if (!string.IsNullOrEmpty(Request["insid"]))
            {
                DataLib.BLL.InStorage bll = new InStorage();
                DataLib.Model.InStorage model = new DataLib.Model.InStorage();
                model = bll.GetModel(int.Parse(Request["insid"]));
                flag = bindOutid((int)model.outid);
            }
            return flag;

        }
        private bool bindOutid(int outid)
        {
            bool flag = true; ;
            DataLib.BLL.Productions bll = new Productions();
            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            DataLib.Model.InStorage insmodel = bll.GetRsidByOutid(outid);

            DataLib.BLL.OutStorage outBll = new DataLib.BLL.OutStorage();
            DataLib.BLL.InStorage inBll = new DataLib.BLL.InStorage();


            outModel = outBll.GetModel(outid);
            if (outModel != null && (outModel.status == 14 || outModel.status == 0))
            {
               
                if (outModel.status == 14)
                {
                    btnAudioPass.Enabled = true;
                    btnCanel.Enabled = true;
                }
                else if (outModel.status == 0)
                {
                    btnAudioPass.Enabled = false;
                    btnCanel.Enabled = true;

                }
                hidoutid.Value = outid.ToString();
                hidoutsid.Value = hidoutid.Value;
                inputLoadNum.InnerHtml = outModel.VLoadNum;
                txtSrcOutSid.InnerHtml = outModel.outStorageNo;

                if (insmodel != null)
                {
                    hidinsid.Value = insmodel.id.ToString();
                    hidwrlno.Value = insmodel.wrlno;
                    spanWrlNo.InnerHtml = insmodel.wrlno;
                    labFlNo.InnerHtml = insmodel.flno;
                    labShNo.InnerHtml = insmodel.shno;
                    ShowFLData((int)insmodel.id);
                }
                spanTotal.InnerHtml = outModel.totalWeight.ToString();
                ShowBarCodeData((int)outModel.id);
            }
            else
            {
                flag = false;
            }
            return flag;
        }


        private void ShowFLData(int instroingid)
        {
            //辅料绑定
            DataTable dtfl = new DataLib.BLL.ins_fuliao().GetList("instroageid=" + instroingid).Tables[0];
            reptFl.DataSource = dtfl;
            reptFl.DataBind();

            //损耗绑定
            DataTable dtsh = new DataLib.BLL.ins_sunhao().GetList("instroageid=" + instroingid.ToString().Trim()).Tables[0];
            reptSh.DataSource = dtsh;
            reptSh.DataBind();
        }

        private void ShowBarCodeData(int outid)
        {
            DataLib.BLL.Productions bll = new Productions();

            spanlf.InnerHtml = bll.GetStrFormulae(outid, "W");
            spanrs.InnerHtml = bll.GetStrFormulae(outid, "S");
            DataTable dt = new DataTable();
            dt = bll.GetBarcodeList(outid, "RS").Tables[0];

            rept.DataSource = dt;
            rept.DataBind();
        }

        protected void btnAudioPass_Click(object sender, EventArgs e)
        {
            DataLib.BLL.Productions bll = new Productions();
            int insid = 0;
            insid = bll.SubmitAudio(int.Parse(Request["insid"]), "");
            if (insid > 0)
            {
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "提交成功，请选择打印", "SelectReport.aspx?insid=" + insid.ToString());
            }
        }

        protected void btnCanel_Click(object sender, EventArgs e)
        {

            DataLib.BLL.OutStorage bll = new DataLib.BLL.OutStorage();
            DataLib.Model.OutStorage model = new DataLib.Model.OutStorage();
            model = bll.GetModel(int.Parse(hidoutsid.Value));
            model.status = 13;
            if (bll.Update(model))
            {
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "退回成功，请选择打印", "/Storage/WaitInStorageList.aspx");
            }
        }
    }
}